using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Claims;
using VotingSystem.Models;

namespace VotingSystem.ViewModels
{
    public class VoteViewModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "请输入投票标题")]
        [StringLength(200, ErrorMessage = "标题长度不能超过200个字符")]
        [Display(Name = "标题")]
        public string Title { get; set; } = string.Empty;

        [StringLength(1000, ErrorMessage = "描述长度不能超过1000个字符")]
        [Display(Name = "描述")]
        public string Description { get; set; } = string.Empty;

        [Required(ErrorMessage = "请设置投票截止时间")]
        [Display(Name = "截止时间")]
        [DataType(DataType.DateTime)]
        public DateTime EndTime { get; set; }

        [Display(Name = "是否公开")]
        public bool IsPublic { get; set; } = true;

        [Required(ErrorMessage = "请至少添加两个投票选项")]
        [MinLength(2, ErrorMessage = "请至少添加两个投票选项")]
        [Display(Name = "投票选项")]
        public List<VoteOptionViewModel> Options { get; set; } = new List<VoteOptionViewModel>();

        public string? CreatorName { get; set; }
        public DateTime CreateTime { get; set; }
        public bool IsActive { get; set; }
        public int TotalVotes { get; set; }
        public bool IsEnded { get; set; }
        public bool HasVoted { get; set; }

        [Display(Name = "投票类型")]
        public VotingSystem.Models.VoteType Type { get; set; } = VotingSystem.Models.VoteType.Single;

        [Display(Name = "是否匿名")]
        public bool IsAnonymous { get; set; }

        [Display(Name = "是否需要登录")]
        public bool RequireLogin { get; set; } = true;

        [Display(Name = "每人可投票数")]
        [Range(1, 10, ErrorMessage = "每人可投票数必须在1-10之间")]
        public int MaxVotesPerUser { get; set; } = 1;

        [Display(Name = "投票结果可见性")]
        public VotingSystem.Models.ResultVisibility ResultVisibility { get; set; } = VotingSystem.Models.ResultVisibility.Always;

        [Display(Name = "投票密码")]
        [StringLength(50, ErrorMessage = "密码长度不能超过50个字符")]
        public string? Password { get; set; }

        [Display(Name = "允许修改投票")]
        public bool AllowModify { get; set; }

        [Display(Name = "投票截止后是否显示结果")]
        public bool ShowResultsAfterEnd { get; set; } = true;

        public string? AccessToken { get; set; }

        public int UserVoteCount { get; set; }
        public bool CanVote { get; set; }

        public bool IsCreator { get; set; }

        public bool CanViewResults => ResultVisibility switch
        {
            VotingSystem.Models.ResultVisibility.Always => true,
            VotingSystem.Models.ResultVisibility.AfterVote => HasVoted || IsCreator,
            VotingSystem.Models.ResultVisibility.AfterEnd => IsEnded || IsCreator,
            VotingSystem.Models.ResultVisibility.CreatorOnly => IsCreator,
            _ => false
        };

        [NotMapped]
        public ClaimsPrincipal? User { get; set; }

        public List<ParticipantViewModel>? Participants { get; set; }

        public List<UserVoteRecordViewModel> MyVoteRecords { get; set; } = new();

        public bool NeedLogin { get; set; }
    }

    public class VoteOptionViewModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "请输入选项标题")]
        [StringLength(200, ErrorMessage = "标题长度不能超过200个字符")]
        [Display(Name = "标题")]
        public string Title { get; set; } = string.Empty;

        [StringLength(500, ErrorMessage = "描述长度不能超过500个字符")]
        [Display(Name = "描述")]
        public string Description { get; set; } = string.Empty;

        [Display(Name = "图片URL")]
        public string? ImageUrl { get; set; }

        public int Votes { get; set; }
        public int VoteCount => Votes;
        public double Percentage { get; set; }

        [Display(Name = "评分范围")]
        [Range(1, 10, ErrorMessage = "评分范围必须在1-10之间")]
        public int? MaxRating { get; set; }

        [Display(Name = "评分说明")]
        public string? RatingDescription { get; set; }

        public double AverageRating { get; set; }
    }

    public class VoteCreateViewModel
    {
        [Required(ErrorMessage = "请输入投票标题")]
        [StringLength(200, ErrorMessage = "标题长度不能超过200个字符")]
        [Display(Name = "标题")]
        public string Title { get; set; } = string.Empty;

        [StringLength(1000, ErrorMessage = "描述长度不能超过1000个字符")]
        [Display(Name = "描述")]
        public string Description { get; set; } = string.Empty;

        [Required(ErrorMessage = "请设置投票截止时间")]
        [Display(Name = "截止时间")]
        [DataType(DataType.DateTime)]
        public DateTime EndTime { get; set; }

        [Display(Name = "是否公开")]
        public bool IsPublic { get; set; } = true;

        [Required(ErrorMessage = "请至少添加两个投票选项")]
        [MinLength(2, ErrorMessage = "请至少添加两个投票选项")]
        [Display(Name = "投票选项")]
        public List<VoteOptionCreateViewModel> Options { get; set; } = new List<VoteOptionCreateViewModel>();

        [Display(Name = "投票类型")]
        public VotingSystem.Models.VoteType Type { get; set; } = VotingSystem.Models.VoteType.Single;

        [Display(Name = "是否匿名")]
        public bool IsAnonymous { get; set; }

        [Display(Name = "是否需要登录")]
        public bool RequireLogin { get; set; } = true;

        [Display(Name = "每人可投票数")]
        [Range(1, 10, ErrorMessage = "每人可投票数必须在1-10之间")]
        public int MaxVotesPerUser { get; set; } = 1;

        [Display(Name = "投票结果可见性")]
        public VotingSystem.Models.ResultVisibility ResultVisibility { get; set; } = VotingSystem.Models.ResultVisibility.Always;

        [Display(Name = "投票密码")]
        [StringLength(50, ErrorMessage = "密码长度不能超过50个字符")]
        public string? Password { get; set; }

        [Display(Name = "允许修改投票")]
        public bool AllowModify { get; set; }

        [Display(Name = "投票截止后是否显示结果")]
        public bool ShowResultsAfterEnd { get; set; } = true;
    }

    public class ParticipantViewModel
    {
        public string Name { get; set; } = string.Empty;
        public string? AvatarUrl { get; set; }
        public DateTime VoteTime { get; set; }
    }

    public class UserVoteRecordViewModel
    {
        public string OptionTitle { get; set; } = "";
        public DateTime VoteTime { get; set; }
        public int? Rating { get; set; }
    }
} 